// pages/settlebill/settlebill.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    billsInProcess: [],
    billConsume: [],
    nop: ["0", "1", "2", "3", "4"],
    nopIndex: 1,
    empty: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // var existed = false
    // var app = getApp()

    // this.setData({
    //   billsInProcess: app.globalData.billsInProcess
    // })

    // if (parseInt(options.billLeft) === 0) {
    //   wx.showToast({
    //     title: '该账单已结算完毕',
    //     icon: 'none',
    //     duration: 1000
    //   })
    //   setTimeout(() => {
    //     wx.navigateBack()
    //   }, 1000)
    //   return
    // }

    // app.globalData.billsInProcess.forEach(item => {
    //   if (item.id === parseInt(options.billId)) {
    //     wx.showToast({
    //       title: '该账单已经在结算列表中',
    //       icon: 'none',
    //       duration: 1000
    //     })
    //     existed = true
    //     return
    //   }
    // })
    // if (!existed) {
    //   app.globalData.billsInProcess.push({
    //     id: parseInt(options.billId),
    //     name: options.billName,
    //     value: options.billValue,
    //     left: options.billLeft,
    //     consume: parseInt(options.billLeft)
    //   })
    //   console.log(this.data.billsInProcess)
    // }
  },

  switchChange: function(e) {
    var app = getApp()

    app.globalData.billsInProcess.forEach((item, index) => {
      if (item.id === parseInt(e.target.id)) {
        app.globalData.billsInProcess.splice(index, 1)
        return
      }
    })
    this.setData({
      billsInProcess: app.globalData.billsInProcess
    })
  },

  getBillLeft: function(id) {
    var app = getApp()
    var left = -1
    app.globalData.billsInProcess.forEach((item, index) => {
      if (parseInt(item.id) === parseInt(id)) {
        left = parseInt(item.left)
        return
      }
    })
    return left
  },

  getBillIndex: function(id) {
    var app = getApp()
    var i = -1
    app.globalData.billsInProcess.forEach((item, index) => {
      if (parseInt(item.id) === parseInt(id)) {
        i = index
        return
      }
    })
    return i
  },

  setBillConsume: function(id, consume) {
    var app = getApp()
    var i = -1
    app.globalData.billsInProcess.forEach((item, index) => {
      if (parseInt(item.id) === parseInt(id)) {
        i = index
        return
      }
    })
    if (i >= 0) {
      app.globalData.billsInProcess[i].consume = consume
    }
  },

  bindNopChange: function(e) {
    this.setData({
      nopIndex: e.detail.value
    })
  },

  bindConsumeChange: function(e) {
    if (e.detail.value === '') {
      this.setBillConsume(e.target.id, this.getBillLeft(e.target.id))
      return
    }
    if (parseInt(e.detail.value) > this.getBillLeft(e.target.id)) {
      var left = this.getBillLeft(e.target.id)
      this.setBillConsume(e.target.id, left)
      return left
    }
    this.setBillConsume(e.target.id, parseInt(e.detail.value))
  },

  bindClearAll: function(e) {
    var that = this
    wx.showModal({
      title: '确定',
      content: '确定清空结算列表?',
      success: function(res) {
        if (res.confirm) {
          var app = getApp()
          app.globalData.billsInProcess = []
          that.setData({
            billsInProcess: app.globalData.billsInProcess
          })
        }
      }
    })
  },

  bindCheckout: function(e) {
    var app = getApp()
    var that = this

    // console.log({
    //   bills: app.globalData.billsInProcess,
    //   nop: parseInt(that.data.nop[that.data.nopIndex])
    // })

    wx.showModal({
      title: '确定',
      content: '确定结算当前列表?',
      success: function(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.apiUrl + '/check_bills',
            method: 'POST',
            data: {
              bills: app.globalData.billsInProcess,
              nop: parseInt(that.data.nop[that.data.nopIndex])
            },
            header: {
              'content-type': 'application/json'
            },
            success: function(res) {
              if (res.statusCode === 200) {
                // console.log(res.data)
                wx.showToast({
                  title: '提交成功',
                  icon: 'success',
                  duration: 2000
                })
                wx.navigateTo({
                  url: 'msg_success?average_value=' + res.data.data,
                })
                app.globalData.billsInProcess = []
                that.setData({
                  billsInProcess: app.globalData.billsInProcess
                })
              } else {
                wx.navigateTo({
                  url: 'msg_fail'
                })
              }
            }
          })
        }
      },

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var app = getApp()
    this.setData({
      billsInProcess: app.globalData.billsInProcess
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})